package com.framework.controller.system;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.framework.controller.index.BaseController;
import com.framework.entity.AuthInfoFormMap;
import com.framework.entity.CorpAuthFormMap;
import com.framework.entity.DSAuthInfoFormMap;
import com.framework.entity.DSCorpAuthFormMap;
import com.framework.entity.DatasourceFormMap;
import com.framework.entity.ProvAccTokenFormMap;
import com.framework.entity.SuiteinfoFormMap;
import com.framework.entity.factory.FormmapFactory;
import com.framework.entity.view.DSAuthInfoViewFormMap;
import com.framework.entity.view.DSCorpAuthViewFormMap;
import com.framework.entity.view.DSProvAtViewFormMap;
import com.framework.entity.view.UsersourceViewFormMap;
import com.framework.util.Common;
import com.framework.util.WXUtil;

import net.sf.json.JSONObject;

/**
 * 
 * @author 李品良:
 * @version 创建时间：2016年8月15日 下午3:12:27 类说明
 */
@Controller
@RequestMapping("/interface_wx/")
public class InterfaceWXController extends BaseController {

	/**
	 * 传入，电话，企业名字，企业编号
	 * 
	 * @author 李品良 创建时间：2016年8月25日 下午2:13:29
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("registeEnterprise")
	public synchronized String registeEnterprise(Model model, HttpServletRequest request) throws Exception {
		SuiteinfoFormMap suiteinfoFormMap=getMapformDataMap(SuiteinfoFormMap.class, getMapFromRequest());
		
		String suite_id=suiteinfoFormMap.getStr("suite_id");
		Object wx_return_info=suiteinfoFormMap.get("wx_return_info");
		
		JSONObject jsonObject2=JSONObject.fromObject(wx_return_info);
		
		String corp_id= suiteinfoFormMap.get("corp_id")!=null?suiteinfoFormMap.getStr("corp_id"):null;
		if (Common.isEmpty(corp_id)) {
			return "corp_id is empty";
		}
		DatasourceFormMap datasourceFormMap2=FormmapFactory.getBeanFormMap(DatasourceFormMap.class);
		datasourceFormMap2.put("corp_id", corp_id);
		List<DatasourceFormMap> list=datasourceFormMap2.findByNames();
		
		if (list.size()==1) {
			DSCorpAuthViewFormMap dsCorpAuthViewFormMap=FormmapFactory.getBeanFormMap(DSCorpAuthViewFormMap.class);
			dsCorpAuthViewFormMap.put("suite_id", suite_id);
			dsCorpAuthViewFormMap.put("corp_id", corp_id);
			dsCorpAuthViewFormMap=dsCorpAuthViewFormMap.findbyFrist();
			bindEnterprise(list.get(0),jsonObject2,suite_id);
			return "bindEnterprise is success";
		}else if (list.size()==0) {
			try {
				DataclientController dataclientController = new DataclientController();
				return dataclientController.addEntity(model, request);
			} catch (Exception e) {
				e.printStackTrace();
				return "failed";
			}
		}
		return "failed";
	}

	@ResponseBody
	@RequestMapping("findEnUser")
	/**
	 *  查询企业是否有该用户
	 *	@author Lyon
	 *创建时间：2016年11月9日 下午8:25:31
	 * @return
	 */
	public boolean findEnUser() {
		UsersourceViewFormMap usersourceViewFormMap=getMapformDataMap(UsersourceViewFormMap.class, getMapFromRequest());
		if (usersourceViewFormMap.size()==0) {
			return false;
		}
		boolean flag=usersourceViewFormMap.findbyFrist()==null?false:true;
		return flag;
	}
	
	
	/**
	 * 绑定企业
	 * 站定默认参数，id，然后会自动保存相对应的关联表
	 * 
	 * @author Lyon 创建时间：2016年11月9日 上午9:41:39
	 * @param datasourceFormMap
	 * @return
	 * @throws Exception
	 */
	@Transactional
	public String bindEnterprise(DatasourceFormMap datasourceFormMap, JSONObject jsonObject2, String suiteid) throws Exception {
		HashMap<String, Object> hashMap=new HashMap<>();
		
		WXUtil.saveAuthCropInfo(jsonObject2, suiteid,hashMap);
		
		DSAuthInfoFormMap dsAuthInfoFormMap = FormmapFactory.getBeanFormMap(DSAuthInfoFormMap.class);
		DSCorpAuthFormMap dsCorpAuthFormMap = FormmapFactory.getBeanFormMap(DSCorpAuthFormMap.class);

		@SuppressWarnings("unchecked")
		List<Object> ai_id_list=(List<Object>) hashMap.get("ai_id_list");
		Object ca_id=hashMap.get("ca_id");
		Object ds_id=datasourceFormMap.get("id");
		
		for (Object ai_id : ai_id_list) {
			dsAuthInfoFormMap = FormmapFactory.getBeanFormMap(DSAuthInfoFormMap.class);
			dsAuthInfoFormMap.put("ds_id", ds_id);
			dsAuthInfoFormMap.put("ai_id", ai_id);
			dsAuthInfoFormMap.replace();
		}
		
		dsCorpAuthFormMap.put("ds_id", ds_id);
		dsCorpAuthFormMap.put("ca_id", ca_id);
		dsCorpAuthFormMap.replace();
		
//		dsProvAtFormMap.put("ds_id", ds_id);
//		dsProvAtFormMap.put("ds_pa_id", 1);
//		dsProvAtFormMap.save();
		
		return "success";
	}

	/**
	 * 绑定软件
	 * 前提是存在该企业和授权，否则不进行 
	 * 参数corp_id
	 * 
	 * @author Lyon 创建时间：2016年11月8日 下午6:38:43
	 * @param corp_id 
	 * @param suite_id 
	 * @return
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 */
	public String bindSoftware(String suite_id, String corp_id) throws InstantiationException, IllegalAccessException {
		DSCorpAuthViewFormMap dsCorpAuthViewFormMap=FormmapFactory.getBeanFormMap(DSCorpAuthViewFormMap.class);
		dsCorpAuthViewFormMap.put("suite_id", suite_id);
		dsCorpAuthViewFormMap.put("corp_id", corp_id);
		dsCorpAuthViewFormMap=dsCorpAuthViewFormMap.findbyFrist();
		if (dsCorpAuthViewFormMap == null) {
			return null;
		} else {
			// 要插入什么信息
		}
		return null;
	}

	/**
	 * 绑定 企业微信ID
	 * 
	 * @author 李品良 创建时间：2016年10月27日 下午4:05:44
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("bindEnCorpId")
	public String bindEnCorpId() throws Exception {
		DatasourceFormMap datasourceFormMap = getMapformDataMap(DatasourceFormMap.class, getMapFromRequest());
		if (datasourceFormMap.containsKey("corp_id") && datasourceFormMap.size() == 2) {
			String corp_id = datasourceFormMap.getStr("corp_id");
			datasourceFormMap.remove("corp_id");
			datasourceFormMap = datasourceFormMap.findbyFrist();
			datasourceFormMap.put("corp_id", corp_id);
			datasourceFormMap.update();
			return "success";
		}
		return "failed";
	}

	/**
	 * 微信登录接口
	 * 参数corp_id
	 * @author 李品良
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("getDBInfoViaCorp")
	public List<DatasourceFormMap> getDBInfoViaCorp(HttpServletRequest request) throws Exception {
		String corp_id = request.getParameter("corp_id");
		if (Common.isEmpty(corp_id)) {
			return null;
		} else {
			DatasourceFormMap datasourceFormMap = FormmapFactory.getBeanFormMap(DatasourceFormMap.class);
			datasourceFormMap.put("corp_id", corp_id);
			List<DatasourceFormMap> list = datasourceFormMap.findByNames();
			if (list.size()>1) {
				return null;
			}
			return list;
		}
	}

	@ResponseBody
	@RequestMapping("getDSAuthInfoView")
	public DSAuthInfoViewFormMap getDSAuthInfoView() throws Exception {
		DSAuthInfoViewFormMap dsAuthInfoViewFormMap = getMapformDataMap(DSAuthInfoViewFormMap.class,
				getMapFromRequest());
		return dsAuthInfoViewFormMap.findbyFrist();
	}

	@ResponseBody
	@RequestMapping("getDSCorpAuthView")
	public DSCorpAuthViewFormMap getDSCorpAuthView() throws Exception {
		DSCorpAuthViewFormMap dsCorpAuthViewFormMap = getMapformDataMap(DSCorpAuthViewFormMap.class,
				getMapFromRequest());
		return dsCorpAuthViewFormMap.findbyFrist();
	}

	@ResponseBody
	@RequestMapping("getDSProvAtView")
	public DSProvAtViewFormMap getDSProvAtView() throws Exception {
		DSProvAtViewFormMap dsProvAtViewFormMap = getMapformDataMap(DSProvAtViewFormMap.class, getMapFromRequest());
		return dsProvAtViewFormMap.findbyFrist();
	}

	@ResponseBody
	@RequestMapping("getSuiteinfo")
	public SuiteinfoFormMap getSuiteinfo() throws Exception {
		SuiteinfoFormMap suiteinfoFormMap = getMapformDataMap(SuiteinfoFormMap.class, getMapFromRequest());
		suiteinfoFormMap = suiteinfoFormMap.findbyFrist();
		return suiteinfoFormMap;
	}

	@ResponseBody
	@RequestMapping("getAllSuiteinfo")
	public List<SuiteinfoFormMap> getAllSuiteinfo() throws Exception {
		SuiteinfoFormMap suiteinfoFormMap = new SuiteinfoFormMap();
		return suiteinfoFormMap.findByAll();
	}

	@ResponseBody
	@RequestMapping("saveAuthInfo")
	public String saveAuthInfo() throws Exception {
		AuthInfoFormMap authInfoFormMap = getMapformDataMap(AuthInfoFormMap.class, getMapFromRequest());
		authInfoFormMap.save();
		return "success";
	}

	@ResponseBody
	@RequestMapping("saveCorpAuth")
	public String saveCorpAuth() throws Exception {
		CorpAuthFormMap corpAuthFormMap = getMapformDataMap(CorpAuthFormMap.class, getMapFromRequest());
		corpAuthFormMap.save();
		return "success";
	}

	@ResponseBody
	@RequestMapping("saveProvAuth")
	public String saveProvAuth() throws Exception {
		ProvAccTokenFormMap provAuthFormMap = getMapformDataMap(ProvAccTokenFormMap.class, getMapFromRequest());
		provAuthFormMap.save();
		return "success";
	}

	@ResponseBody
	@RequestMapping("saveSuiteinfo")
	public String saveSuiteinfo() throws Exception {
		SuiteinfoFormMap suiteinfoFormMap = getMapformDataMap(SuiteinfoFormMap.class, getMapFromRequest());
		suiteinfoFormMap.save();
		return "success";
	}

	@ResponseBody
	@RequestMapping("updateAuthInfo")
	public String updateAuthInfo() throws Exception {
		AuthInfoFormMap authInfoFormMap = getMapformDataMap(AuthInfoFormMap.class, getMapFromRequest());
		authInfoFormMap.update();
		return "success";
	}

	@ResponseBody
	@RequestMapping("updateCorpAuth")
	public String updateCorpAuth() throws Exception {
		CorpAuthFormMap corpAuthFormMap = getMapformDataMap(CorpAuthFormMap.class, getMapFromRequest());
		corpAuthFormMap.update();
		return "success";
	}

	@ResponseBody
	@RequestMapping("updateProvAccToken")
	public String updateProvAccToken() throws Exception {
		ProvAccTokenFormMap provAccTokenFormMap = getMapformDataMap(ProvAccTokenFormMap.class, getMapFromRequest());
		provAccTokenFormMap.update();
		return "success";
	}

	@ResponseBody
	@RequestMapping("updateSuiteinfo")
	public String updateSuiteinfo() throws Exception {
		SuiteinfoFormMap suiteinfoFormMap = getMapformDataMap(SuiteinfoFormMap.class, getMapFromRequest());
		suiteinfoFormMap.update();
		return "success";
	}
	
	@ResponseBody
	@RequestMapping("delAuthInfoByAttr")
	public String delAuthInfoByAttr() throws Exception {
		AuthInfoFormMap suiteinfoFormMap = getMapformDataMap(AuthInfoFormMap.class, getMapFromRequest());
		suiteinfoFormMap.deleteByNames();
		return "success";
	}
	
	@ResponseBody
	@RequestMapping("cancelCorpAuth")
	public String cancelCorpAuth() throws Exception {
		CorpAuthFormMap dsCorpAuthFormMap = getMapformDataMap(CorpAuthFormMap.class, getMapFromRequest());
		dsCorpAuthFormMap = dsCorpAuthFormMap.findbyFrist();
		if (dsCorpAuthFormMap==null) {
			return "not exist";
		}
		dsCorpAuthFormMap.deleteById(dsCorpAuthFormMap.getStr("id"));
		AuthInfoFormMap authInfoFormMap = new AuthInfoFormMap();
		authInfoFormMap.put("suite_id", dsCorpAuthFormMap.get("suite_id"));
		authInfoFormMap.put("corpid", dsCorpAuthFormMap.get("corpid"));
		authInfoFormMap.deleteByNames();
		return "success";
	}

	
	@ResponseBody
	@RequestMapping("getDSAuthInfoViewByCorpAndSuit")
	public List<DSAuthInfoViewFormMap> getDSAuthInfoViewByCorpAndSuit() throws Exception {
		DSAuthInfoViewFormMap dsAuthInfoViewFormMap = getMapformDataMap(DSAuthInfoViewFormMap.class,
				getMapFromRequest());
		return dsAuthInfoViewFormMap.findByNames();
	}
}
